//
//  DBTabBarController.h
//  DuoBao
//
//  Created by zyt on 2016-02-19 11:23:28
//  Copyright (c) 2016年 zyt. All rights reserved.
//

#import "DBTabBarController.h"

#import "HomeViewController.h"
#import "DiscoveryViewController.h"
#import "SettingsViewController.h"

#import "UIColor+Util.h"
#import "UIImage+Category.h"

@interface DBTabBarController ()<UITabBarControllerDelegate> {
}

@property (nonatomic, strong) HomeViewController* homeVC;
@property (nonatomic, strong) DiscoveryViewController* discoveryVC;
@property (nonatomic, strong) SettingsViewController* settingsVC;

@end

@implementation DBTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
      
    self.homeVC = [HomeViewController new];
    self.discoveryVC = [DiscoveryViewController new];
    self.settingsVC = [SettingsViewController new];

    self.viewControllers = @[[self addNavigationItemForViewController:_homeVC],
                             [self addNavigationItemForViewController:_discoveryVC],
                             [self addNavigationItemForViewController:_settingsVC]];
    
    NSArray *tags = @[@(TabItemTypeHome), @(TabItemTypeLast), @(TabItemTypeList), @(TabItemTypeMine)];
    NSArray *titles = @[@"首页", @"发现", @"消息", @"我的"];
    NSArray *images = @[@"tabbar-home", @"tabbar-revealed", @"tabbar-list", @"tabbar-me"];
    [self.tabBar.items enumerateObjectsUsingBlock:^(UITabBarItem *item, NSUInteger idx, BOOL *stop) {
        item.tag = [tags[idx] intValue];

        // 设置Tabbar文字
        [item setTitle:titles[idx]];
        
        // 设置Tabbar图片
        UIImage* nImage = [UIImage imageNamed:images[idx]];
        nImage = [nImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [item setImage:nImage];
        
        UIImage* hImage = [UIImage imageNamed:[images[idx] stringByAppendingString:@"-selected"]];
        hImage = [hImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [item setSelectedImage:hImage];
    }];
    
    self.delegate = self;

    // [self.tabBar setBackgroundImage:[[UIImage alloc] init]];
    
    // 选中背景颜色
    self.tabBar.selectionIndicatorImage = [UIImage imageWithColor:[UIColor colorWithHex:0xfaf3f3] andSize:CGSizeMake(SCREEN_WIDTH/titles.count, self.tabBar.frame.size.height)];

    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}


- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [self initBadgeValue];
}



#pragma mark - Init functions

- (UINavigationController *)addNavigationItemForViewController:(UIViewController *)viewController{
    
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    navigationController.navigationBar.translucent = NO;
    return navigationController;
}


- (void)onClickMenuButton
{
    BMLog(@"onClickMenuButton");
    // [self.sideMenuViewController presentLeftMenuViewController];
}

- (void)pushSearchViewController
{
    BMLog(@"onClickMenuButton");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITabBarDelegate

- (void)tabbarToIndex:(int)idx {
    self.selectedIndex = idx;
}

/**
 *  初始化显示提醒红色图标
 */
- (void)initBadgeValue{
    // 更新清单的未读数
    int unreadCount = 0;
    [self updateBadgeNumWithIndex:TabItemTypeList count:unreadCount];
}

/**
 *  更新Tabbar上面的小红点
 *
 *  @param index 索引位置，从0开始
 *  @param count TabBar上面小红点显示的未读数
 */
- (void)updateBadgeNumWithIndex:(int)index count:(int)count{
    
    NSString* stringValue = nil;
    if (count > 0) {
        if (count > 99) {
            stringValue = @"99+";
        }else {
            stringValue = [NSString stringWithFormat:@"%d", count];
        }
    }
    
    if (index >=0 && index<self.tabBar.items.count) {
        UITabBarItem* tabBarItem = self.tabBar.items[index];
        tabBarItem.badgeValue = stringValue;
    }

}


#pragma mark - UITabBarDelegate

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    TabItemType type = item.tag;
    
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
    return YES;
}


@end
